package fr.univ.surveyonline.constraints;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import fr.univ.surveyonline.model.User;
import fr.univ.surveyonline.model.allEnum.AgeEnum;
import fr.univ.surveyonline.model.allEnum.Categorie;
import fr.univ.surveyonline.model.allEnum.Region;

public class SortConstraints {
/*
	public static Map<Region,Set<User>> getRegionNummer(List<User> set) {
		Map<Region,Set<User>> map = new HashMap<Region,Set<User>>();
		
		for (User u : set) {
			Set<User> s = map.get(u.getRegion());
			if (s == null) {
				s = new HashSet<User>();
				map.put(u.getRegion(), s);
			}
			s.add(u);
		}
		
		return map;
	}
	
	public static Map<Categorie,Set<User>> getCategorieNummer(List<User> set) {
		Map<Categorie,Set<User>> map = new HashMap<Categorie,Set<User>>();

		for (User u : set) {
			Set<User> s = map.get(u.getCategorie());
			if (s == null) {
				s = new HashSet<User>();
				map.put(u.getCategorie(), s);
			}
			s.add(u);
		}
		
		return map;
	}

	public static Map<String,Set<User>> getSexeNummer(List<User> set) {
		Map<String,Set<User>> map = new HashMap<String,Set<User>>();

		for (User u : set) {
			Set<User> s = map.get(u.getSexe());
			if (s == null) {
				s = new HashSet<User>();
				map.put(u.getSexe(), s);
			}
			s.add(u);
		}
		
		return map;
	}
	
	public static Map<AgeEnum,Set<User>> getAgeNummer(List<User> set,Date d) {
		Map<AgeEnum,Set<User>> map = new HashMap<AgeEnum,Set<User>>();

		Calendar surveyDate = Calendar.getInstance();
		surveyDate.setTime(d);
		for (User u : set) {
			Calendar birthdayDate = Calendar.getInstance();
			birthdayDate.setTime(u.getBirthday());
			AgeEnum a = getAgeEnum(birthdayDate,surveyDate);
			
			Set<User> s = map.get(a);
			if (s == null) {
				s = new HashSet<User>();
				map.put(a, s);
			}
			s.add(u);
		}
		
		return map;
	}
 
	private static AgeEnum getAgeEnum(Calendar birthdayDate, Calendar surveyDate) {
		if (birthdayDate.after(surveyDate)) {
			return null;
		}
		
		for (AgeEnum a : AgeEnum.values()) {
			Calendar newCalendar = Calendar.getInstance();
			newCalendar.setTime(surveyDate.getTime());
			
			newCalendar.add(Calendar.YEAR, -a.getMinimumAge());
			if (newCalendar.before(birthdayDate.getTime())) {
				continue;
			}
			
			if (a.getMaximumAge() == null) {
				return a;
			}
			newCalendar.setTime(surveyDate.getTime());
			newCalendar.add(Calendar.YEAR, -a.getMaximumAge());
			if (birthdayDate.before(newCalendar.getTime())) {
				continue;
			}
			return a;
		}
		return null;
	}*/
}
